/**
 * 字典管理初始化
 */
var Type = {
    id: "TypeTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Type.initColumn = function () {
    return [
        {field: 'selectItem', checkbox: true},
        {title: 'id', field: 'id',  align: 'center', valign: 'middle'},
        {title: '名称', field: 'name', align: 'center', valign: 'middle', sortable: true},
        {title: '价格', field: 'price', align: 'center', valign: 'middle', sortable: true},
        {title: '天数', field: 'days', align: 'center', valign: 'middle', sortable: true},
        {title: '创建时间', field: 'create_time', align: 'center', valign: 'middle', sortable: true},
        {title: '修改时间', field: 'update_time', align: 'center', valign: 'middle', sortable: true},

    ];
};

/**
 * 检查是否选中
 */
Type.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length === 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        Type.seItem = selected[0];
        return true;
    }
};

/**
 * 批量检查是否选中
 */
Type.batchCheck = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        Type.seItem = selected;
        return true;
    }
};
/**
 * 点击添加字典
 */
Type.openAddDict = function () {
    var index = layer.open({
        type: 2,
        title: '添加支付参数',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/vipType/vipType_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看字典详情
 */
Type.openDictDetail = function () {
    if (this.batchCheck()) {
        if(Type.seItem.length === 0){
            Feng.info("请先选中表格中的某一记录！");
            return false;
        }else if(Type.seItem.length >1){
            Feng.info("选择多个关键词无法修改");
            return false;
        }else {
            var index = layer.open({
                type: 2,
                title: '字符参数详情',
                area: ['800px', '420px'], //宽高
                fix: false, //不固定
                maxmin: true,
                content: Feng.ctxPath + '/vipType/vipType_edit/' + Type.seItem[0].id
            });
            this.layerIndex = index;
        }
    }
};
/**
 * 删除字典
 */
Type.delete = function () {
    if (this.batchCheck()) {

        var operation = function(){
            var ajax = new $ax(Feng.ctxPath + "/vipType/delete", function (data) {
                Feng.success("删除成功!");
                Type.table.refresh();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            var ids = "";
            for (var i = 0;i < Type.seItem.length;i++) {
                if(i < Type.seItem.length - 1) {
                    ids += Type.seItem[i].id + ",";
                } else {
                    ids += Type.seItem[i].id;
                }
            }
            ajax.set("vipTypeId", ids);
            ajax.start();
        };
        var names = "";
        for (var i = 0; i < Type.seItem.length;i++) {
            if(i < Type.seItem.length - 1) {
                names += Type.seItem[i].name +"  ";
            } else {
                names += Type.seItem[i].name;
            }
        }
        Feng.confirm("是否刪除 "  + names + "?", operation);
    }
};

/**
 * 查询字典列表
 */
Type.search = function () {
    var queryData = {};
    queryData['keyword'] = $("#keyword").val();
    Type.table.refresh({query: queryData});
};

$(function () {
    var defaultColunms = Type.initColumn();
    var table = new BSTable(Type.id, "/vipType/list", defaultColunms);
    table.setPaginationType("client");
    Type.table = table.init();
});
